import { useState, useRef } from "react";
import { Button, Space, Spin  } from "antd";
import { ControlType as TYPE } from "@components";
import { useMainContext } from '@contexts/MainContext'
import { RULE_REQUIRED } from "@rules";

import { findEmerMasterList } from '@apis/massMsgApi'

const rules = [RULE_REQUIRED]
var Mock = require('mockjs')

export const fields = [
  { name: 'title', label: '消息标题', controlType: TYPE.INPUT },
  { name: 'emerName', label: "应急事项", controlType: TYPE.INPUT },
  { name: 'sendStatus', label: "消息状态", controlType: TYPE.SELECT, codeKey: 'msgSendStatus' },
  // { name: 'sendMode', label: "发送方式", controlType: TYPE.SELECT, codeKey: 'expireToInsuranceCode' },
  { name: 'auditStatus', label: "审核状态", controlType: TYPE.SELECT, codeKey: 'examineStatus' },
  { name: 'sendTime', label: '发送时间', controlType: TYPE.DATE_PICKER },
];

export const GetColumns = (edit, detailsRef, audit) => {
  const { isButtomShow } = useMainContext()

  return [
    { title: '消息标题', dataIndex: 'title', key: 'title', ellipsis: true, open: true, width: 200 },
    { title: '消息内容', dataIndex: 'content', key: 'content', ellipsis: true, open: true, width: 200 },
    { title: '创建时间', dataIndex: 'createTime', key: 'createTime', ellipsis: true, open: true, width: 200 },
    { title: '应急事项', dataIndex: 'emerName', key: 'emerName', ellipsis: true, open: true, width: 200 },
    { title: '归属机构', dataIndex: 'unitName', key: 'unitName', ellipsis: true, open: true, width: 200 },
    { title: '消息总量', dataIndex: 'msgCount', key: 'msgCount', open: true, width: 200 },
    { title: '消息状态', dataIndex: 'sendStatusName', key: 'sendStatusName', open: true, width: 200 },
    { title: '发送进度', dataIndex: 'sendSchedule', key: 'sendSchedule', open: true, width: 200 },
    { title: '发送时间', dataIndex: 'sendTime', key: 'sendTime', open: true, width: 200 },
    { title: '审核状态', dataIndex: 'auditStatusName', key: 'auditStatusName', open: true, width: 200 },
    { title: '审核描述', dataIndex: 'auditDesc', key: 'auditDesc', ellipsis: true, open: true, width: 200 },
    {
      title: "操作",
      dataIndex: 'operationCol',
      key: 'operationCol',
      open: true,
      fixed: 'right',
      width: 260, 
      render: (_, record) => {
        return (
          <Space>
            {isButtomShow('280302') && <Button type="primary" onClick={() => edit(record)}>编辑</Button>}
            <Button type="primary" ghost onClick={() => detailsRef.current.showDetails(record)}>详情</Button>
            {record.auditStatus !== 'ReviewedSuccess' ? (
              <>
                {isButtomShow('280303') && <Button type="primary" onClick={() => audit(record)}>审批</Button>}
              </>
            ) : null}
          </Space>
        )
      }
    }
  ]

}

const setUnitCodeOrName = async (value, list=[], modalFormRef) => {
  if (!value) return
  const item = list.find(item => item.value === value)
  modalFormRef.current?.setFieldValue('unitCode', item.unitCode)
  modalFormRef.current?.setFieldValue('unitName', item.unitName)
}
export const GetModalFields = (masterList, modalFormRef, disabled=false) => {
  // const { getCode } = useMainContext()
  // const newExter = getCode('msgTempTag')?.options.reduce((total, current) => total + current.value+ '，', '')

  return [
    { name: 'unitCode', label: '机构code', controlType: TYPE.INPUT, mode: 'hidden' },
    { name: 'unitName', label: '机构名称', controlType: TYPE.INPUT, mode: 'hidden' },
    { name: 'title', label: '消息标题', controlType: TYPE.INPUT, showCount:true, maxLength: 20, rules },
    { 
      name: 'emerMasterId', label: "应急事项", controlType: TYPE.SELECT, rules,
      options: masterList, allowClear: true, showSearch: true,
      optionFilterProp: "children", disabled,
      filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
      onSelect: value => setUnitCodeOrName(value, masterList, modalFormRef)
    },
    { 
      name: 'content', label: "消息内容", controlType: TYPE.TEXTAREA, style: { height: 150 },
      formitemprops:{rules: [RULE_REQUIRED], extra: "消息内容支持匹配替换字段：[姓名]"} },
  ];
}

export const DetailsModelBodyHeight = 760

export const auditModalFields = [
  { 
    name:'auditStatus', label:'审核状态', controlType:TYPE.SELECT, rules: [RULE_REQUIRED], codeKey: 'examineStatus',
    uselessOptions: ['toBeReviewed','Reviewing']
  },
  { name:'auditDesc', label:'审核描述', controlType:TYPE.TEXTAREA, rules: [RULE_REQUIRED] }
]